package com.tanhua.management.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.LogManagementApi;
import com.tanhua.dubbo.server.pojo.LogRecord;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.management.vo.LogParamVo;
import com.tanhua.management.vo.LogShowVo;
import com.tanhua.management.vo.PageResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogManagerService {

    @Reference(version = "1.0.0")
    private LogManagementApi logManagementApi;

    public PageResult logPage(LogParamVo logParamVo) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(logParamVo.getPage());
        pageResult.setPagesize(logParamVo.getPagesize());

        PageInfo<LogRecord> pageInfo = logManagementApi.logPage(logParamVo.getPage(),
                logParamVo.getPagesize(), logParamVo.getSortProp(), logParamVo.getSortOrder(),
                logParamVo.getUid());

        List<LogRecord> records = pageInfo.getRecords();

        List<LogShowVo> list = new ArrayList<>();

        for (LogRecord logRecord : records) {
            LogShowVo logShowVo = new LogShowVo();
            logShowVo.setId(logRecord.getUserId());
            logShowVo.setLogTime(logRecord.getLogTime());
            logShowVo.setPlace(logRecord.getLogAddress());
            logShowVo.setEquipment(logRecord.getEquipment());

            list.add(logShowVo);
        }

        pageResult.setPages(pageInfo.getPages());
        pageResult.setCounts(pageInfo.getTotal());

        pageResult.setItems(list);
        return pageResult;
    }
}
